﻿using MoetutuServer.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoetutuServer.Pixiv.ProcessImg
{
    class Helper
    {/// <summary>
    /// 获取用户信息
    /// </summary>
    /// <param name="Filename"></param>
    /// <returns></returns>
        public static ArtInfo GetUserId(String Filename) {

            String UserID = "";
            List<String> AllTag = new List<string>();
            String Url = "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=" + GetImgId(Filename);
            String Shtml =WebHelper.HttpGet(Url) ;

            foreach (String line in HtmlHelper.GetLinkVlaueList(Shtml, "//div[@class='usericon']/a")) {
                UserID = line.Replace("member.php?id=","");
            }

            foreach (String Line in HtmlHelper.GetTextValueList(Shtml, "//ul[@class='inline-list']/li[@class='tag']/a[2]")) {
                String TagTemp = TagShort(Line) ;
                if (TagTemp != ""&&!AllTag.Contains(TagTemp))
                    AllTag.Add(TagTemp);


            }

            ArtInfo artInfo = new ArtInfo();
            artInfo.TagList1 = AllTag;
            Console.WriteLine(UserID);
            artInfo.UserId1 = UserID;


                return artInfo;
        }
        /// <summary>
        /// 删除无用的标签
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public static string TagShort(string line)
        {
            line = line.Trim();
            line = TextHelper.ReplaceText(line, "[0-9]{2,9}users入り", "");
          
            Console.WriteLine(line);
            return line;
        }
        /// <summary>
        /// 缩短并且处理整个列表
        /// </summary>
        /// <param name="TempList"></param>
        /// <returns></returns>
        public static List<string> TagListShort(List<string> TempList) {
            HashSet<string> hs = new HashSet<string>(TempList); //此时已经去掉重复的数据保存在hashset中
            List<string> Temp = new List<string>();
            int count = 0;
            foreach (string line in hs.ToList<string>()) {
                string TempText = TagShort(line).Trim();
                if (WebSite.Pixiv.TagHelper.SearchCnTag(TempText) != "")
                {
                    Console.WriteLine("已有汉化，不重复汉化，跳过");
                    continue;
                }
                if (!Temp.Contains(TempText))
                {
                    Console.WriteLine("不存在，进行数量判断,已扫描{0}，总量{1}",count++, hs.Count);
                    int TAGIMGCount = GetTagCount(TempText);
                    if (TAGIMGCount > 100)
                    {
                       
                        Temp.Add(TempText+ "图片数量：" + TAGIMGCount);
                        Console.WriteLine("已添加{0}个", Temp.Count);
                    }
                    else
                        Console.WriteLine("分数{0}过低，跳过", TAGIMGCount);
                }


            }
            return Temp;
        }
        /// <summary>
        /// 获取图片数量
        /// </summary>
        /// <param name="tempText"></param>
        /// <returns></returns>
        public static int GetTagCount(string tempText)
        {
         
            string Shtml = WebHelper.HttpGet("https://www.pixiv.net/search.php?s_mode=s_tag&word="+TextHelper.TranscodingToUtf8(tempText));
            string Text = HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(Shtml, "//span[@class='count-badge']"));
            Text = TextHelper.RegexText(Text, "[0-9]{0,}");
            Console.WriteLine("个数为："+Text);
            return TextHelper.StringToInt(Text);
        }

        public static string GetImgId(String Filename) {

            return TextHelper.ReplaceText(Path.GetFileNameWithoutExtension(Filename), "_p[0-9]{0,5}", "");
        }
        public struct ArtInfo {
            String UserId;
            List<String> TagList;
            public string UserId1 { get => UserId; set => UserId = value; }
            public List<string> TagList1 { get => TagList; set => TagList = value; }
        }
    }
}
